package com.jt.assistant.custom_view;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jt.assistant.R;
import com.jt.assistant.base.GlobalConfig;
import com.jt.assistant.ui.DecodeActivity;
import com.yzp.mvvmlibrary.base.MessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import static com.jt.assistant.base.GlobalConfig.SCAN_TYPE;

public class ScanEditText extends LinearLayout {

    private ImageView mIvScan;
    public EditText mEtScan;
    private Button mBtSearch;
    private String mScanType;

    public ScanEditText(Context context) {
        super(context);
        initView(context, null);
    }

    public ScanEditText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public ScanEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }


    private void initView(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.scan_edit_text, this, true);
        mIvScan = view.findViewById(R.id.iv_scan);
        mEtScan = view.findViewById(R.id.et_scan);
        mBtSearch = view.findViewById(R.id.bt_search);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScanEditText);
        String hint = typedArray.getString(R.styleable.ScanEditText_hint);
        boolean isShownButton = typedArray.getBoolean(R.styleable.ScanEditText_isShownButton,true);
        boolean enableCamera = typedArray.getBoolean(R.styleable.ScanEditText_enableCamera, true);
        if (hint != null) {
            mEtScan.setHint(hint);
        }
        mIvScan.setVisibility(enableCamera ? View.VISIBLE : View.GONE);
        mBtSearch.setVisibility(isShownButton ? View.VISIBLE : View.GONE);
        typedArray.recycle();
        init();
    }

    public void init() {
        mEtScan.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEND
                        || actionId == EditorInfo.IME_ACTION_DONE
                        || (keyEvent != null
                        && KeyEvent.KEYCODE_ENTER == keyEvent.getKeyCode()
                        && KeyEvent.ACTION_DOWN == keyEvent.getAction())) {
                    //处理事件
                    if (onScanListener != null)
                        onScanListener.scanListener(getString());
                }
                return false;
            }
        });

        mBtSearch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onScanListener != null)
                    onScanListener.scanListener(getString());
            }
        });
    }

    public void initCamera(final AppCompatActivity baseActivity, String type) {
        mScanType = type;
        EventBus.getDefault().register(this);
        mIvScan.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(baseActivity, DecodeActivity.class);
                intent.putExtra(SCAN_TYPE, mScanType);
                baseActivity.startActivity(intent);
            }
        });
    }

    public String getString() {
        return mEtScan.getText().toString().trim()+"";
    }

    public void setString(String code) {
        mEtScan.setText(code);
    }

    public void clearData() {
        mEtScan.setText("");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mEtScan.requestFocus();
            }
        }, 60);
    }


    OnScanListener onScanListener;

    public interface OnScanListener {
        void scanListener(String code);
    }

    public void setOnScanListener(OnScanListener onScanListener) {
        this.onScanListener = onScanListener;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void message(MessageEvent messageEvent) {
        if (messageEvent.getType().equals(mScanType)) {
            setString(messageEvent.getMsg());
            if (onScanListener != null)
                onScanListener.scanListener(getString());
        }
    }

    public void unregisterEventBus() {
        EventBus.getDefault().unregister(this);
    }

}
